package com.wpf.system.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wpf.system.entity.BalanceJournal;
import com.wpf.system.entity.vo.BalanceJournalVO;
import com.wpf.system.entity.vo.PageVO;
import com.wpf.system.entity.vo.PointJournalVO;
import com.wpf.system.service.BalanceJournalService;
import com.wpf.system.service.PointJournalService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/api/journal")
public class JournalController {
    @Resource
    private BalanceJournalService balanceJournalService;

    @Resource
    private PointJournalService pointJournalService;

    @GetMapping("/turnover")
    public BigDecimal getTurnover(@RequestParam("date") String time,@RequestParam("offset") int offset){
        Date date = DateUtil.offsetDay(DateUtil.parse(time),offset);
        QueryWrapper<BalanceJournal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",1);
        queryWrapper.between("create_time", DateUtil.beginOfDay(date),DateUtil.endOfDay(date));
        queryWrapper.select("sum(balance_change) as balance");
        if(balanceJournalService.getOne(queryWrapper)!=null){
            return balanceJournalService.getOne(queryWrapper).getBalance();
        }else{
            return BigDecimal.valueOf(0);
        }
    }

    @GetMapping("/balance")
    public PageVO<BalanceJournalVO> getBalanceJournalList(
            @RequestParam("aid") Long aid,
            @RequestParam(value = "type",required=false) String type,
            @RequestParam(value = "startTime",required=false) String startTime,
            @RequestParam(value = "endTime",required=false) String endTime,
            @RequestParam(value = "page",required=false) int page,
            @RequestParam(value = "pageSize",required=false) int pageSize
    ){
        PageVO<BalanceJournalVO> balanceJournalVOPageVO = new PageVO<>();
        balanceJournalVOPageVO.setList(balanceJournalService.getBalanceJournal(aid,type,startTime,endTime,Math.max((page - 1) * pageSize,0),pageSize));
        balanceJournalVOPageVO.setTotal(balanceJournalService.getBalanceJournal(aid,type,startTime,endTime,null,null).size());
        return balanceJournalVOPageVO;
    }

    @GetMapping("/point")
    public PageVO<PointJournalVO> getPointJournalList(
            @RequestParam("aid") Long aid,
            @RequestParam(value = "type",required=false) String type,
            @RequestParam(value = "startTime",required=false) String startTime,
            @RequestParam(value = "endTime",required=false) String endTime,
            @RequestParam(value = "page",required=false) int page,
            @RequestParam(value = "pageSize",required=false) int pageSize
    ){
        PageVO<PointJournalVO> pointJournalVOPageVO = new PageVO<>();
        pointJournalVOPageVO.setList(pointJournalService.getPointJournal(aid,type,startTime,endTime,Math.max((page - 1) * pageSize,0),pageSize));
        pointJournalVOPageVO.setTotal(pointJournalService.getPointJournal(aid,type,startTime,endTime,null,null).size());
        return pointJournalVOPageVO;
    }
}
